import os
import pymongo
import common.gyblog as log
import manage_db.query_db as dm

PATH = '\\\\lcnas\\xishu16T_2022\\07-FC2\\'
PATH_1 = '\\\\lcnas\\xishu4told\\07-FC2_temp\\'

'''
更新数据库，使得数据库和视频目录同步
'''


def check_is_in_db():
    # 连接数据库
    db_client = pymongo.MongoClient(dm.CONNECTION_STR)
    db_database = db_client['movies_record']
    db_col = db_database['fc2_manage_record']

    root_dirs = os.listdir(PATH) + os.listdir(PATH_1)
    for root_dir in root_dirs:
        fc2_num = root_dir.replace('-4K', '')

        # 查询是否在数据库里
        query = {'num': fc2_num}
        query_ans = list(db_col.find(query))
        if len(query_ans) == 0:
            dic = {'num': fc2_num, 'tor': '', 'is_save': 'True'}
            db_col.insert_one(dic)
        if len(query_ans) > 1:
            print('{}:有重复数据'.format(fc2_num))
    db_client.close()


def check_is_save():
    # 连接数据库
    db_client = pymongo.MongoClient(dm.CONNECTION_STR)
    db_database = db_client['movies_record']
    db_col = db_database['fc2_manage_record']

    root_dirs = os.listdir(PATH) + os.listdir(PATH_1)
    # 查出所有数据
    query_ans = list(db_col.find())
    for q in query_ans:
        num = q['num']
        if num not in root_dirs:
            # 更新数据库
            query = {'num': num}
            new_values = {"$set": {"is_save": 'False'}}
            db_col.update_one(query, new_values)
        else:
            # 更新数据库
            query = {'num': num}
            new_values = {"$set": {"is_save": 'True'}}
            db_col.update_one(query, new_values)

    db_client.close()


if __name__ == '__main__':
    check_is_in_db()
    check_is_save()
